const { Controller } = require('egg')

class ShareController extends Controller {
  // 获得文章数据
  async getArticleList() {
    const { ctx, app } = this
    ctx.validate({
      page: { type: 'int', min: 1, required: true, desc: '页码不正确' },
      size: { type: 'int', min: 1, required: false }
    })
    const { page, size = 5 } = ctx.query
    const offset = (page - 1) * size
    // 查询文章数据 并计数时间最新的在最前面
    let articleList = await app.model.Article.findAll({
      offset,
      limit: size,
      order: [['created_at', 'DESC']],
      status: 1,
      include: [
        {
          model: app.model.ArticleDetail
        }
      ]
    })

    articleList = JSON.parse(JSON.stringify(articleList))
    articleList.forEach((item) => {
      item.detail_id = item.article_detail.id
      item.article_detail = JSON.parse(item.article_detail.content)
    })

    ctx.apiSuccess(articleList)
  }
  // 根据文章id获得评论
  async getCommentList() {
    const { ctx, app } = this
    ctx.validate({
      page: { type: 'int', min: 1, required: true, desc: '页码不正确' },
      size: { type: 'int', min: 1, required: false },
      article_id: { type: 'int', required: true, desc: '文章id不能为空' }
    })
    const { article_id, page, size = 5 } = ctx.query
    const offset = (page - 1) * size
    // 查询文章数据 并计数时间最新的在最前面
    const commentList = await app.model.Comment.findAll({
      offset,
      limit: size,
      order: [['created_at', 'DESC']],
      where: { article_id, status: 1 }
    })
    ctx.apiSuccess(commentList)
  }
  // 发布文章评论
  async addComment() {
    const { ctx, app } = this

    // 参数验证
    ctx.validate({
      content: {
        type: 'string',
        required: true,
        desc: '评论内容不能为空'
      },
      article_id: {
        type: 'int',
        required: true,
        desc: '文章id不能为空'
      },
      username: {
        type: 'string',
        required: true,
        desc: '用户名不能为空'
      },
      avatar: {
        type: 'string',
        required: true,
        desc: '头像不能为空'
      }
    })
    const { content, article_id, username, avatar } = ctx.request.body
    // 检查对应id的文章是否存在
    const article = await app.model.Article.findByPk(article_id)
    if (!article) ctx.throw(400, '对应id文章不存在')
    // 添加评论
    const res = await app.model.Comment.create({
      content,
      article_id,
      username,
      avatar
    })
    if (!res) ctx.throw(500, '评论失败')
    ctx.apiSuccess(res)
  }
  // 给文章点赞
  async addLikeArticle() {
    const { ctx, app } = this
    ctx.validate({
      article_id: { type: 'int', required: true, desc: '文章id不能为空' }
    })
    const { article_id } = ctx.request.query

    console.log(article_id)

    const res = await app.model.Article.findByPk(article_id)
    if (!res) ctx.throw(400, '对应id文章不存在')
    res.likes += 1
    await res.save()
    ctx.apiSuccess('点赞成功')
  }
  // 增加文章的预览量
  async addViewArticle() {
    const { ctx, app } = this
    ctx.validate({
      article_id: { type: 'int', required: true, desc: '文章id不能为空' }
    })
    const { article_id } = ctx.request.query
    const res = await app.model.Article.findByPk(article_id)
    if (!res) ctx.throw(400, '对应id文章不存在')
    res.views += 1
    await res.save()
    ctx.apiSuccess('增加预览量成功')
  }
  // 获得随记列表
  async getLiftRecord() {
    const { app, ctx } = this
    ctx.validate({
      page: { type: 'int', min: 1, required: true, desc: '页码不正确' },
      size: { type: 'int', min: 1, required: false }
    })

    const { page, size = 5 } = ctx.query
    const offset = (page - 1) * size
    let liftRecordList = await app.model.LiftRecord.findAll({
      offset,
      limit: size,
      order: [['created_at', 'DESC']],
      status: 1
    })

    liftRecordList = JSON.parse(JSON.stringify(liftRecordList))
    liftRecordList.forEach((item) => {
      item.content = JSON.parse(item.content)
    })
    ctx.apiSuccess(liftRecordList)
  }

  // 获得文章数据  admin
  async getArticleAdminList() {
    const { ctx, app } = this
    ctx.validate({
      page: { type: 'int', min: 1, required: true, desc: '页码不正确' },
      size: { type: 'int', min: 1, required: false }
    })
    const { page, size = 5 } = ctx.query
    const offset = (page - 1) * size
    // 查询文章数据 并计数时间最新的在最前面 status: 1
    let articleList = await app.model.Article.findAndCountAll({
      offset,
      limit: size,
      order: [['created_at', 'DESC']],
      status: 1,
      include: [
        {
          model: app.model.ArticleDetail
        }
      ]
    })

    articleList = JSON.parse(JSON.stringify(articleList))
    articleList.rows.forEach((item) => {
      item.detail_id = item.article_detail.id
      item.article_detail = JSON.parse(item.article_detail.content)
    })

    ctx.apiSuccess(articleList)
  }

  // 修改文章数据  admin
  async updateArticle() {
    const { ctx, app } = this

    const content = ctx.request.body
    console.log(content)
    if (!content.id) return ctx.apiFail('文章id不能为空')
    if (!content.detail_id) return ctx.apiFail('文章详情id不能为空')
    // 根据id找到对应文章
    const article = await app.model.Article.findByPk(content.id)
    if (!article) return ctx.apiFail('对应id文章不存在')
    // 修改文章内容
    article.title = content.title
    article.content = content.content
    article.poster = content.poster
    await article.save()
    // 根据detail_id找到对应文章详情
    const detail = await app.model.ArticleDetail.findByPk(content.detail_id)
    if (!detail) return ctx.apiFail('对应id文章详情不存在')
    // 修改文章详情内容
    detail.content = JSON.stringify(content.article_detail)
    await detail.save()
    ctx.apiSuccess('修改成功')
  }

  // 删除文章数据  admin
  async deleteArticle() {
    const { ctx, app } = this
    ctx.validate({
      id: { type: 'int', required: true, desc: '文章id不能为空' },
      detail_id: { type: 'int', required: true, desc: '文章详情id不能为空' }
    })
    const { id, detail_id } = ctx.request.body
    // 根据id找到对应文章
    const article = await app.model.Article.findByPk(id)
    if (!article) return ctx.apiFail('对应id文章不存在')
    await article.destroy()
    // 根据detail_id找到对应文章详情
    const detail = await app.model.ArticleDetail.findByPk(detail_id)
    if (!detail) return ctx.apiFail('对应id文章详情不存在')
    await detail.destroy()
  }

  // 新增文章 admin
  async addArticle() {
    const { ctx, app } = this
    // ctx.validate({
    //   formData: { type: Object, required: true, desc: '内容不能为空' }
    // })
    const formData = ctx.request.body
    console.log(formData, '--------------')
    if (!formData.title) return ctx.apiFail('文章标题不能为空')
    if (!formData.desc) return ctx.apiFail('文章内容不能为空')
    if (!formData.poster) return ctx.apiFail('文章封面不能为空')

    // 增加文章详情信息
    let articleDetail = null
    if (formData.article_detail.length > 0) {
      articleDetail = await app.model.ArticleDetail.create({
        content: JSON.stringify(formData.article_detail)
      })
      if (!articleDetail) {
        return ctx.apiFail('文章详情信息增加失败')
      }
    }
    // 增加文章信息
    const article = await app.model.Article.create({
      title: formData.title,
      content: formData.content,
      poster: formData.poster,
      desc: formData.desc,
      stattus: 1,
      detail_id: articleDetail ? articleDetail.id : 0
    })
    if (!article) {
      articleDetail.destroy()
      return ctx.apiFail('文章信息增加失败')
    }

    ctx.apiSuccess('增加成功')
  }

  // 获得随记列表 admin
  async getLiftRecordAdmin() {
    const { app, ctx } = this
    ctx.validate({
      page: { type: 'int', min: 1, required: true, desc: '页码不正确' },
      size: { type: 'int', min: 1, required: false }
    })

    const { page, size = 5 } = ctx.query
    const offset = (page - 1) * size
    let liftRecordList = await app.model.LiftRecord.findAndCountAll({
      offset,
      limit: size,
      order: [['created_at', 'DESC']],
      status: 1
    })

    liftRecordList = JSON.parse(JSON.stringify(liftRecordList))
    liftRecordList.rows.forEach((item) => {
      item.content = JSON.parse(item.content)
    })
    console.log(liftRecordList.rows[0])
    ctx.apiSuccess(liftRecordList)
  }

  // 删除随记 admin
  async deleteLiftRecord() {
    const { ctx, app } = this
    ctx.validate({
      id: { type: 'int', required: true, desc: '随记id不能为空' }
    })
    const { id } = ctx.request.query
    const liftRecord = await app.model.LiftRecord.findByPk(id)
    if (!liftRecord) return ctx.apiFail('对应id随记不存在')
    await liftRecord.destroy()
    ctx.apiSuccess('删除成功')
  }

  // 更新随记 admin
  async updateLiftRecord() {
    const { ctx, app } = this
    ctx.validate({
      id: { type: 'int', required: true, desc: '随记id不能为空' },
      title: { type: 'string', required: true, desc: '随记标题不能为空' },
      content: { type: 'string', required: true, desc: '随记内容不能为空' },
      image: { type: 'string', required: true, desc: '图片不能为空' }
    })
    const { id, title, content, image } = ctx.request.body
    const liftRecord = await app.model.LiftRecord.findByPk(id)
    if (!liftRecord) return ctx.apiFail('对应id随记不存在')
    const res = await liftRecord.update({
      title,
      image,
      content,
      status: 1
    })
    if (!res) return ctx.apiFail('更新失败')
    ctx.apiSuccess('更新成功')
  }

  // 增加随记内容
  async addLiftRecord() {
    const { ctx, app } = this
    ctx.validate({
      title: { type: 'string', required: true, desc: '随记标题不能为空' },
      image: { type: 'string', required: false },
      content: { type: 'string', required: true, desc: '随记内容不能为空' }
    })
    const { title, image, content } = ctx.request.body
    const liftRecord = await app.model.LiftRecord.create({
      title,
      image,
      content,
      status: 1
    })
    if (!liftRecord) return ctx.apiFail('增加失败')
    ctx.apiSuccess(liftRecord)
  }
}

module.exports = ShareController
